package com.example.frame;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private ImageFrame mImageFrame;

    private ImageFrameAdapter mImageFrameAdapter;

    private Button mBtnUpdate;

    private String[] images = {
            "http://img.xqxtp.com/201702/09/12/43nwouqwrsh.jpg",
            "http://img.xqxtp.com/201702/09/12/nxaffe0j0t1.jpg",
            "http://img.xqxtp.com/201702/09/12/1itogktstyc.jpg",
            "http://img.xqxtp.com/201702/09/12/1vmzh0la2po.jpg",
            "http://img.xqxtp.com/201702/09/12/d5jklxcut1y.jpg"
    };

    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initAdapter();
        initListener();
    }

    private void initView() {
        mImageFrame = (ImageFrame) findViewById(R.id.iv_frame);
        mBtnUpdate = (Button) findViewById(R.id.btn_update);
    }

    private void initAdapter() {
        mImageFrameAdapter = new ImageFrameAdapter(this);
        mImageFrame.setAdapter(mImageFrameAdapter);
    }

    private void initListener() {
        mBtnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateAdapter();
            }
        });
    }

    private void updateAdapter() {
        ImageResp temp = new ImageResp();
        temp.setUrl(images[index++ % 5]);
        mImageFrameAdapter.addItem(temp);
        mImageFrameAdapter.notifyDataSetChanged();
    }
}
